package com.yoo.ons.function.me

import android.net.Uri
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.yoo.chat.kit.RongIM
import com.yoo.module_base.utils.GlideUtil
import com.yoo.module_base.utils.SaveDeviceUtil
import com.yoo.module_base.utils.StatusBarUtil
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.ons.base.BaseVMFragment
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.databinding.FragmentMeBinding
import com.yoo.ons.dialog.HelpCenterPopup
import com.yoo.ons.function.MainViewModel
import com.yoo.ons.function.album.PersonAlbumActivity
import com.yoo.ons.function.me.like.MyLikeListActivity
import com.yoo.ons.function.me.viewmodel.MeViewModel
import com.yoo.ons.function.premium.PremiumActivity
import com.yoo.ons.function.profile.EditProfileActivity
import com.yoo.ons.function.settings.SettingsActivity
import com.yoo.ons.model.PremiumModel
import com.yoo.ons.model.QueryBenefitsModel
import io.rong.imlib.model.UserInfo

/**
 * @Author andy
 * @Date 2024/5/28 20:23
 */
class MeFragment : BaseVMFragment<FragmentMeBinding, MeViewModel>(
    FragmentMeBinding::inflate,
    MeViewModel::class.java
) {
    private val mainViewModel by lazy {
        ViewModelProvider(requireActivity())[MainViewModel::class.java]
    }

    override fun initData() {
        binding?.topView?.setPadding(0, StatusBarUtil.getStatusBarHeight(activity), 0, 0)
        getILikeCount()
        getQueryBenefits()
        getMemberSubscription()
        //getUserInfo()
        mainViewModel.userInfoLiveData.observe(viewLifecycleOwner) { t ->
            t?.apply {
                RongIM.getInstance().refreshUserInfoCache(
                    UserInfo(t.userCode, t.nickName, Uri.parse(t.avatarUrl))
                )
//                binding?.editPAlbumContainer?.isVisible = !SaveDeviceUtil.getPPVSwitch()

                binding?.threeEquityContainer?.isVisible = !SaveDeviceUtil.getPPVSwitch()
                binding?.horizontalFlContainer?.isVisible = SaveDeviceUtil.getPPVSwitch()
                binding?.txtMemberUntil?.isVisible = t.isMember
                binding?.txtMemberSubButton?.isVisible = !t.isMember
                binding?.imgMeHead?.let {
                    GlideUtil.setImageView(t.avatarUrl, it, activity)
                }
                binding?.imgAvatarBg?.let {
                    GlideUtil.setImageView(t.avatarUrl, it, activity, true, 10, 10)
                }
                binding?.txtMeName?.text = t.nickName
                binding?.txtMeAge?.text = "${t.age}"

                if (t.isMember) {
                    binding?.txtMemberUntil?.text =
                        if (t.autoRenew == 1) getString(com.yoo.module_res.R.string.auto_subscription) else "Until: ${t.expiryDate}"
                }

                binding?.editProfileContainer?.setOnClickCallback {
                    EditProfileActivity.start(activity, t)
                }
                binding?.editPAlbumContainer?.setOnClickCallback {
                    PersonAlbumActivity.start(activity,t.isMember,t.privateAlbums?:-1)
                }
            }
        }
    }

    override fun initListener() {
        binding?.helpCenterContainer?.setOnClickCallback {
            activity?.let { it1 -> HelpCenterPopup(it1).showPopupWindow() }
        }
        binding?.settingsContainer?.setOnClickListener {
            SettingsActivity.start(activity)
        }
        binding?.clILikeContainer?.setOnClickCallback {
            MyLikeListActivity.start(activity)
        }
        binding?.memberSubContainer?.setOnClickCallback {
            PremiumActivity.start(activity)
        }

    }

    /**
     * 查询i like数量
     */
    fun getILikeCount() {
        mViewModel?.getILikeCount(object : HttpRequestListener<Int>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
            }

            override fun success(t: Int?) {
                t?.apply {
                    binding?.txtILikeNumber?.isVisible = t > 0
                    binding?.txtILikeNumber?.text = "$t"
                }
            }

        })
    }

    /**
     * 查询权益剩余数量
     */
    fun getQueryBenefits() {
        mViewModel?.getQueryBenefits(object : HttpRequestListener<List<QueryBenefitsModel>>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
            }

            override fun success(t: List<QueryBenefitsModel>?) {
                t?.apply {
                    binding?.txtHorFlNumber?.text = "${t[0].maxUses}"
                    binding?.txtFlNumber?.text = "${t[0].maxUses}"
                    binding?.txtPpNumber?.text = "${t[1].maxUses}"
                    binding?.txtPvNumber?.text = "${t[2].maxUses}"
                }
            }
        })
    }

    /**
     * 查询会员信息
     */
    fun getMemberSubscription() {
        mViewModel?.getPremiumSubscription(object : HttpRequestListener<PremiumModel>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
            }

            override fun success(t: PremiumModel?) {
                t?.apply {
                    binding?.txtMemberSubTitle?.text = t.memberDescription.tip
                    binding?.txtMemberSubContent?.text = t.memberDescription.content
                }
            }
        })
    }
}